package com.hnjowe.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author Administrator
 * 定义各种时间格式
 */
public class TimeFormat {

        private static TimeFormat instance = null;

        private TimeFormat() {
        }

        /**
         * 返回一个TimeFormat类的单一实例
         * @return m_instance
         */
        public synchronized  static TimeFormat getInstance(){
                if(instance == null){
                        instance = new TimeFormat();
                }
                return instance;
        }
        
    	//获取日期，格式：yyyyMMdd
    	public String getYMD(){
    		Date date=new Date();
    		DateFormat df=new SimpleDateFormat("yyyyMMdd");
    		return df.format(date);
    	}
    	
    	//获取时间，格式：HHmmss
    	public String getHMS(){
    		Date date=new Date();
    		DateFormat df=new SimpleDateFormat("HHmmss");
    		return df.format(date);
    	}

        /**
         * 时间格式：yyyy-mm-dd hh:mi:ss   eg.2006-5-18 8:15:26
         * @return yyyy-mm-dd hh:mi:ss
         */
        public String getDateTime(){
                Calendar c=new GregorianCalendar();
                int year = c.get(Calendar.YEAR);   //年
                int month=c.get(Calendar.MONTH);   //月
                month = month + 1;
                int day=c.get(Calendar.DATE);      //日
                int hour=c.get(Calendar.HOUR);     //小时
                int minute=c.get(Calendar.MINUTE); //分钟
                int second=c.get(Calendar.SECOND); //秒
                int am_pm=c.get(Calendar.AM_PM);
                if(am_pm==1){
                   hour=hour+12;
                   if(hour==24)hour=0;             //小时转换成24小时制
                };
            String yearStr = String.valueOf(year).toString();
            String monthStr = String.valueOf(month).toString();
            String dayStr = String.valueOf(day).toString();
                String hourStr = String.valueOf(hour).toString();
                String minuteStr = String.valueOf(minute).toString();
                String secondStr = String.valueOf(second).toString();

                String hourTime = yearStr + "-" + monthStr + "-" + dayStr + " " + hourStr+":"+minuteStr+":"+secondStr;
                return hourTime;
        }
        
        /**
         * 时间格式：yyyy-mm-dd   eg.2006-05-18
         * @return yyyy-mm-dd
         */
        public String getDate(){
        	Calendar c = new GregorianCalendar();
			int year = c.get(Calendar.YEAR); // 年
			int month = c.get(Calendar.MONTH); // 月
			month = month + 1;
			int day = c.get(Calendar.DATE); // 日
			
			int week = c.get(Calendar.DAY_OF_WEEK);
	
			String yearStr = "";
            String monthStr = "";
            String dayStr = "";
            String weekStr = "";
            
            yearStr = String.valueOf(year).toString();
			
            if(month<10){ //月
                monthStr = "0" + String.valueOf(month).toString();
            }else{
            	monthStr = String.valueOf(month).toString();
          	}

            if(day<10){ //日
                dayStr = "0" + String.valueOf(day).toString();
            }else{
                dayStr = String.valueOf(day).toString();
            }
            
            if(week==1)weekStr = "星期日";
            if(week==2)weekStr = "星期一";
            if(week==3)weekStr = "星期二";
            if(week==4)weekStr = "星期三";
            if(week==5)weekStr = "星期四";
            if(week==6)weekStr = "星期五";
            if(week==7)weekStr = "星期六";
            
			String date = yearStr + "-" + monthStr + "-" + dayStr + "  " + weekStr; 
			return date;
        }

        /**
		 * 时间格式：hh:mi:ss eg. 08:15:26
		 * 
		 * @return hh:mi:ss
		 */
        public String getHourTime(){
                Calendar c=new GregorianCalendar();
                int hour=c.get(Calendar.HOUR);
                int minute=c.get(Calendar.MINUTE);
                int second=c.get(Calendar.SECOND);

                int am_pm=c.get(Calendar.AM_PM);
                if(am_pm==1){
                   hour=hour+12;
                   if(hour==24)hour=0;
                };

                String hourStr;
                String minuteStr;
                String secondStr;

                if(hour<10){ //时
                    hourStr = "0" + String.valueOf(hour).toString();
                }else{
                    hourStr = String.valueOf(hour).toString();
                }

                if(minute<10){ //分
                        minuteStr = "0" + String.valueOf(minute).toString();
                }else{
                        minuteStr = String.valueOf(minute).toString();
                }

                if(second<10){ //秒
                        secondStr = "0" + String.valueOf(second).toString();
                }else{
                        secondStr = String.valueOf(second).toString();
                }

                String dateTime = hourStr+":"+minuteStr+":"+secondStr;
                return dateTime;
        }

        /**
         * 时间格式：hh:mi:ss.ns   eg. 08:15:26.002
         * @return hh:mi:ss.ns
         */
        public String getHourTimeEx(){
                Calendar c=new GregorianCalendar();
                int hour=c.get(Calendar.HOUR);
                int minute=c.get(Calendar.MINUTE);
                int second=c.get(Calendar.SECOND);
                int misecond=c.get(Calendar.MILLISECOND);

                int am_pm=c.get(Calendar.AM_PM);
                if(am_pm==1){
                   hour=hour+12;
                   if(hour==24)hour=0;
                };

                String hourStr;
                String minuteStr;
                String secondStr;
                String misecondStr;

                if(hour<10){ //时
                    hourStr = "0" + String.valueOf(hour).toString();
                }else{
                    hourStr = String.valueOf(hour).toString();
                }

                if(minute<10){ //分
                        minuteStr = "0" + String.valueOf(minute).toString();
                }else{
                        minuteStr = String.valueOf(minute).toString();
                }

                if(second<10){ //秒
                        secondStr = "0" + String.valueOf(second).toString();
                }else{
                        secondStr = String.valueOf(second).toString();
                }
                if(misecond<10){
                        misecondStr = "00" + String.valueOf(misecond).toString();
                }else if((misecond>=10)&&(misecond<100)){
                        misecondStr = "0" + String.valueOf(misecond).toString();
                }else{
                        misecondStr = String.valueOf(misecond).toString();
                }

                String dateTime = hourStr+":"+minuteStr+":"+secondStr+"."+misecondStr;
                return dateTime;
        }


        /**
         * 时间格式：YYYYMMDDHH   eg.2006051808
         * @return YYYYMMDDHH
         */
        public String getTimeSeq(){
                Calendar c=new GregorianCalendar();

                int year = c.get(Calendar.YEAR); //年
                int month=c.get(Calendar.MONTH); //月
                month = month + 1;
                int day=c.get(Calendar.DATE); //日
                int hour=c.get(Calendar.HOUR); //时
                int am_pm=c.get(Calendar.AM_PM);
                if(am_pm==1){
                        hour=hour+12;
                if(hour==24) hour=0;
                };

                String yearStr;
                String monthStr;
                String dayStr;
                String hourStr;

                yearStr = String.valueOf(year).toString();

                if(month<10){ //月
                monthStr = "0" + String.valueOf(month).toString();
                }else{
                        monthStr = String.valueOf(month).toString();
                }

                if(day<10){ //日
                dayStr = "0" + String.valueOf(day).toString();
                }else{
                dayStr = String.valueOf(day).toString();
                }

                if(hour<10){ //时
                hourStr = "0" + String.valueOf(hour).toString();
                }else{
                hourStr = String.valueOf(hour).toString();
                }

                String timeSeq = yearStr + monthStr + dayStr + hourStr;

                return timeSeq;
        }

        /**
         * 时间格式：YYYYMMDD   eg.20060518
         * @return YYYYMMDD
         */
        public String getTimeSeq_1(){
                Calendar c=new GregorianCalendar();

                int year = c.get(Calendar.YEAR); //年
                int month=c.get(Calendar.MONTH); //月
                month = month + 1;
                int day=c.get(Calendar.DATE); //日

                String yearStr;
                String monthStr;
                String dayStr;

                yearStr = String.valueOf(year).toString();

                if(month<10){ //月
                monthStr = "0" + String.valueOf(month).toString();
                }else{
                        monthStr = String.valueOf(month).toString();
                }

                if(day<10){ //日
                dayStr = "0" + String.valueOf(day).toString();
                }else{
                dayStr = String.valueOf(day).toString();
                }

                String timeSeq = yearStr + monthStr + dayStr;

                return timeSeq;
        }

        /**
         * 时间格式：YYYYMMDDHHMISS    eg.20060206214115 (2006年2月6日21点41分15秒)
         * @return YYYYMMDDHHMISS
         */
        public String getTimeSeqEx(){
            Calendar c=new GregorianCalendar();
            int year = c.get(Calendar.YEAR);   //年yyyy
            int month=c.get(Calendar.MONTH);   //月mm
            month = month + 1;
            int day=c.get(Calendar.DATE);      //日dd
            int hour=c.get(Calendar.HOUR);     //时hh
            int minute=c.get(Calendar.MINUTE); //分mi
            int second=c.get(Calendar.SECOND); //秒ss

            int am_pm=c.get(Calendar.AM_PM);
            if(am_pm==1){
              hour=hour+12;
              if(hour==24) hour=0;
            };
            String yearStr;
            String monthStr;
            String dayStr;
            String hourStr;
            String minuteStr;
            String secondStr;

            yearStr = String.valueOf(year).toString().substring(2);

            if(month<10){ //月
              monthStr = "0" + String.valueOf(month).toString();
            }else{
              monthStr = String.valueOf(month).toString();
              }

            if(day<10){ //日
              dayStr = "0" + String.valueOf(day).toString();
            }else{
              dayStr = String.valueOf(day).toString();
            }

            if(hour<10){ //时
               hourStr = "0" + String.valueOf(hour).toString();
            }else{
               hourStr = String.valueOf(hour).toString();
            }

            if(minute<10){ //分
               minuteStr = "0" + String.valueOf(minute).toString();
            }else{
               minuteStr = String.valueOf(minute).toString();
            }

            if(second<10){ //秒
               secondStr = "0" + String.valueOf(second).toString();
            }else{
               secondStr = String.valueOf(second).toString();
            }

            String timeSeq = yearStr + monthStr + dayStr + hourStr + minuteStr + secondStr;
            return timeSeq;
        }

        /**
         * 时间格式：MMDDHHMISS    eg.0206214115 (2月6日21点41分15秒)
         * @return MMDDHHMISS
         */
        public String getTimeSeqEx_1(){
            Calendar c=new GregorianCalendar();

            int month=c.get(Calendar.MONTH);   //月mm
            month = month + 1;
            int day=c.get(Calendar.DATE);      //日dd
            int hour=c.get(Calendar.HOUR);     //时hh
            int minute=c.get(Calendar.MINUTE); //分mi
            int second=c.get(Calendar.SECOND); //秒ss
            int misecond=c.get(Calendar.MILLISECOND); //秒ms

            int am_pm=c.get(Calendar.AM_PM);
            if(am_pm==1){
              hour=hour+12;
              if(hour==24) hour=0;
            };

            String monthStr;
            String dayStr;
            String hourStr;
            String minuteStr;
            String secondStr;
            String misecondStr;

            if(month<10){ //月
              monthStr = "0" + String.valueOf(month).toString();
            }else{
              monthStr = String.valueOf(month).toString();
              }

            if(day<10){ //日
              dayStr = "0" + String.valueOf(day).toString();
            }else{
              dayStr = String.valueOf(day).toString();
            }

            if(hour<10){ //时
               hourStr = "0" + String.valueOf(hour).toString();
            }else{
               hourStr = String.valueOf(hour).toString();
            }

            if(minute<10){ //分
               minuteStr = "0" + String.valueOf(minute).toString();
            }else{
               minuteStr = String.valueOf(minute).toString();
            }

            if(second<10){ //秒
               secondStr = "0" + String.valueOf(second).toString();
            }else{
               secondStr = String.valueOf(second).toString();
            }

            if(misecond<10){
              misecondStr = "000" + String.valueOf(misecond).toString();
            }else if((misecond>=10)&&(misecond<100)){
              misecondStr = "00" + String.valueOf(misecond).toString();
            }else if((misecond>=100)&&(misecond<1000)){
              misecondStr = "0" + String.valueOf(misecond).toString();
            }else{
              misecondStr = String.valueOf(misecond).toString();
            }

            String timeSeq = monthStr + dayStr + hourStr + minuteStr + secondStr + misecondStr;
            return timeSeq;
        }
        
        /**
         * 时间格式：YYMMDDHHMISSxxx    eg.060206214115000 (2006年2月6日21点41分15秒)
         * @return YYMMDDHHMISSxxx
         */
        public String getTimeSequene(){
            Calendar c=new GregorianCalendar();
            int year = c.get(Calendar.YEAR);   //年yyyy
            int month=c.get(Calendar.MONTH);   //月mm
            month = month + 1;
            int day=c.get(Calendar.DATE);      //日dd
            int hour=c.get(Calendar.HOUR);     //时hh
            int minute=c.get(Calendar.MINUTE); //分mi
            int second=c.get(Calendar.SECOND); //秒ss
            int misecond=c.get(Calendar.MILLISECOND); //秒ms

            int am_pm=c.get(Calendar.AM_PM);
            if(am_pm==1){
              hour=hour+12;
              if(hour==24) hour=0;
            };
            String yearStr;
            String monthStr;
            String dayStr;
            String hourStr;
            String minuteStr;
            String secondStr;
            String misecondStr;

            yearStr = String.valueOf(year).toString().substring(2);

            if(month<10){ //月
              monthStr = "0" + String.valueOf(month).toString();
            }else{
              monthStr = String.valueOf(month).toString();
              }

            if(day<10){ //日
              dayStr = "0" + String.valueOf(day).toString();
            }else{
              dayStr = String.valueOf(day).toString();
            }

            if(hour<10){ //时
               hourStr = "0" + String.valueOf(hour).toString();
            }else{
               hourStr = String.valueOf(hour).toString();
            }

            if(minute<10){ //分
               minuteStr = "0" + String.valueOf(minute).toString();
            }else{
               minuteStr = String.valueOf(minute).toString();
            }

            if(second<10){ //秒
               secondStr = "0" + String.valueOf(second).toString();
            }else{
               secondStr = String.valueOf(second).toString();
            }
            
            if(misecond<10){
                misecondStr = "00" + String.valueOf(misecond).toString();
              }else if((misecond>=10)&&(misecond<100)){
                misecondStr = "0" + String.valueOf(misecond).toString();
              //}else if((misecond>=100)&&(misecond<1000)){
                //misecondStr = "0" + String.valueOf(misecond).toString();
              }else{
                misecondStr = String.valueOf(misecond).toString();
              }

            String timeSeq = yearStr + monthStr + dayStr + hourStr + minuteStr + secondStr + misecondStr;
            return timeSeq;
        }
        
        
        
        /**
        * 功能：截取两个日期之间的天数
        * @param String beginDate,String endDate
        * @return int
        * @throws ParseException
        **/
        public int getDays(String beginDate, String endDate){
        	try{
        		SimpleDateFormat sim = new SimpleDateFormat( "yyyy-MM-dd");
            	Date d1 = sim.parse(beginDate); 
            	Date d2 = sim.parse(endDate); 
            	return (int)((d2.getTime() - d1.getTime())/(3600L * 1000 * 24));
        	}catch(Exception e){
        		return 0;
        	}
        }

        /**
        * 描述:一个日期加上一个天数，得到一个新的日期* @param String beginDate, long addDay
        * @return Date
        * @throws ParseException
        **/
        public Date getNewDate(String beginDate, long addDay) throws ParseException {
	        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
	        Date d1 = sim.parse(beginDate.trim()); 
	        long time = d1.getTime();
	        addDay = addDay * 24 * 60 * 60 * 1000;
	        time += addDay;
	        return new Date(time);
        }

        /**
        * 功能：一个日期时间加上分钟数，得到一个新的日期时间* @param String beginDate, long addDay
        * @return Date
        * @throws ParseException
        * @throws ParseException
        **/
        public Date getNewDateTime(String beginDateTime, long addMinutes) throws ParseException{
        	SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	        Date d1 = sim.parse(beginDateTime); 
	        long time = d1.getTime();
	        addMinutes = addMinutes * 60 * 1000;
	        time += addMinutes;
	        return new Date(time);
        }
        
        /**
         * 年月比较
         * @param yearmonth1	年月1,加年数，得最新年月，与年月2比较
         * @param years			年数
         * @param yearmonth2	年月2
         * @return boolean		false/true
         */
        public boolean YearMonthCompare(String yearmonth1, int years, String yearmonth2){
        	boolean result = false;
        	Calendar cal = Calendar.getInstance(); 
    		String arry[]=yearmonth1.split("-"); 
    		int year = Integer.parseInt(arry[0]); 
    		int month = Integer.parseInt(arry[1]);
    		cal.set(Calendar.YEAR, year); 
    		cal.set(Calendar.MONTH, month); 
    		cal.add(Calendar.MONTH, -1); 
    		cal.add(Calendar.YEAR, years); 	//增加years年 
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    		String lastYM = sdf.format(cal.getTime()); //得出最新年月
    		//System.out.println("YM1 = " + lastYM); 
    		//System.out.println("YM2 = " + yearmonth2); 

    		//两个年月比较
            try {
                Date YM1 = sdf.parse(lastYM);
                Date YM2 = sdf.parse(yearmonth2);
                
                if (YM2.getTime() <= YM1.getTime()) {
                	result = true;
                }else{
                	result = false;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        	return result;
        }
        
        
}
